package org.example.myweibo.common.aspect;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Slf4j
public class LogAspect {
    @Before("execution(public org.example.myweibo.common.result.JsonResult org.example.myweibo.controller.*.*(..))")
    public void before(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        String className = joinPoint.getTarget().getClass().getName();
        log.info(className + "." + methodName+"()开始执行");
    }
    @AfterReturning("execution(public org.example.myweibo.common.result.JsonResult org.example.myweibo.controller.*.*(..))")
    public void afterReturning(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        String className = joinPoint.getTarget().getClass().getName();
        log.info(className + "." + methodName+"()执行完毕");
    }
    @AfterThrowing(value = "execution(public org.example.myweibo.common.result.JsonResult org.example.myweibo.controller.*.*(..))", throwing = "throwable")
    public void afterThrowing(JoinPoint joinPoint, Throwable throwable){
        String methodName = joinPoint.getSignature().getName();
        String className = joinPoint.getTarget().getClass().getName();
        log.error(className + "." + methodName+"()执行出错，错误信息：" + throwable.getMessage());
    }
}